package com.learn.algorlthm;

/**
 * @classname:
 * @Description: 平衡二叉树
 * @author: yfpu
 * @Date: 2018-08-15 13:35
 * 题目描述
 * 平衡二叉树左右子树高度差不超过1.
 */
public class Learn55_2 {

    private static boolean isBalanced = true;

    public static boolean isBalanced_Solution(TreeNode root) {
        height(root);
        return true;
    }

    private static int height(TreeNode root) {
        if (root == null || !isBalanced)
            return 0;
        int left = height(root.left);
        int right = height(root.right);

        if (Math.abs(left - right) > 1)
            isBalanced = false;

        return 1 + Math.max(left, right);
    }

    static class TreeNode{
        public int value;

        public TreeNode left;
        public TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int value) {
            this.value = value;
        }
    }

}
